﻿using MediumApi.Db;
using MediumApi.Result;
using MediumApi.Service;
using MediumApi.Tools;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static MediumApi.BaseController;
using static MediumApi.Param.AppParam;
using static MediumApi.Result.AppResult;

namespace MediumApi.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class RoleController : BaseController
    {
        private readonly AppDbContext _context;
        private readonly PermissionService _permissionService;
        public RoleController(AppDbContext context, PermissionService permissionService)
        {
            _context = context;
            _permissionService = permissionService; 
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<OperateResult> AddRole(AddRoleParam param)
        {
            if (param.Id <= 0)
            {
                if (await _permissionService.AddRole(param) > 0) return Success();
            }
            else
            {
                if (await _permissionService.UpdateRole(param) > 0) return Success();
            }
            return Error();
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="UserId">管理员ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<OperateResult> DelRole(int RoleId)
        {
            if (await _permissionService.DelRole(RoleId) > 0) return Success();
            return Error();
        }
        /// <summary>
        /// 查询角色列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<SelectRoleListResult>))]
        public async Task<OperateResult> SelectRoleList()
        {
            return Success(await _permissionService.SelectRoleList());
        }




        /// <summary>
        /// 添加页面
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<OperateResult> AddPageTable(AddPageTableParam param)
        {
            if (param.Id == 0)
            {
                if (await _permissionService.AddPageTable(param) > 0) return Success();
            }
            else 
            {
                if (await _permissionService.UpdatePageTable(param) > 0) return Success();
            }
            return Error();
        }
        /// <summary>
        /// 删除页面
        /// </summary>
        /// <param name="UserId">页面ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<OperateResult> DelPageTable(int PageTableId)
        {
            if (await _permissionService.DelPageTable(PageTableId) > 0) return Success();
            return Error();
        }
        /// <summary>
        /// 查询页面列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<SelectPageTableListResult>))]
        public async Task<OperateResult> SelectPageTableList()
        {
            return Success(await _permissionService.SelectPageTableList());
        }
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<OperateResult> AddMenu(AddMenuParam param)
        {
            if (param.Id <= 0)
            {
                if (await _permissionService.AddMenu(param) > 0) return Success();
            }
            else
            {
                if (await _permissionService.UpdateMenu(param) > 0) return Success();
            }
            return Error();
        }
        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="UserId">页面ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<OperateResult> DelMenu(int MenuId)
        {

            if (await _permissionService.DelMenu(MenuId) > 0) return Success();
            return Error();
        }
        /// <summary>
        /// 查询菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ApiAuthority]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<SelectMenuListResult>))]
        public async Task<OperateResult> SelectMenuList()
        {
            var data = await _permissionService.GetAllMenusAsync(this.RoleID);
            return Success(data);
        }
        /// <summary>
        /// 添加角色菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<OperateResult> AddRolePageTable(AddRoleMenuParam param)
        {

            if (await _permissionService.AddRolePageTable(param) > 0) return Success();
            return Success();
        }
        /// <summary>
        /// 删除角色菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<OperateResult> DelRolePageTable(int RoleMenuId)
        {

            if (await _permissionService.DelRolePageTable(RoleMenuId) > 0) return Success();
            return Error();
        }
        /// <summary>
        /// 查询角色菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<SelectRoleMenuListResult>))]
        public async Task<OperateResult> SelectRolePageTableList(SelectRolePageTableListParam param)
        {

            return Success(await _permissionService.SelectRolePageTableList(param));
        }
        /// <summary>
        /// 查询当前角色菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ApiAuthority]
        public async Task<OperateResult> SelectRoleMenuList()
        {
            List<SelectMenuListResult> MenuList = new List<SelectMenuListResult>();
            MenuList = await _permissionService.GetRoleMenusAsync(this.RoleID);

            List<SelectMenuListResult> BtnList = await _permissionService.btnGetAllMenusAsync(this.RoleID);
            return Success(new { MenuList = MenuList, BtnList = BtnList });
        }
    }
}
